package com.example.forestmanagement;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Switch;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class SettingsActivity extends AppCompatActivity {
    
    private Switch switchLocationUpdate;
    private Switch switchMapSatellite;
    private Switch switchNotification;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
        
        // 初始化视图
        initViews();
        
        // 加载设置
        loadSettings();
    }
    
    private void initViews() {
        // 设置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("设置");
        
        toolbar.setNavigationOnClickListener(v -> finish());
        
        // 初始化开关控件
        switchLocationUpdate = findViewById(R.id.switch_location_update);
        switchMapSatellite = findViewById(R.id.switch_map_satellite);
        switchNotification = findViewById(R.id.switch_notification);
        
        // 设置监听器
        switchLocationUpdate.setOnCheckedChangeListener((buttonView, isChecked) -> {
            saveSettings("location_update", isChecked);
            Toast.makeText(this, 
                    isChecked ? "已开启位置更新" : "已关闭位置更新", 
                    Toast.LENGTH_SHORT).show();
        });
        
        switchMapSatellite.setOnCheckedChangeListener((buttonView, isChecked) -> {
            saveSettings("map_satellite", isChecked);
            Toast.makeText(this, 
                    isChecked ? "已切换到卫星地图" : "已切换到普通地图", 
                    Toast.LENGTH_SHORT).show();
        });
        
        switchNotification.setOnCheckedChangeListener((buttonView, isChecked) -> {
            saveSettings("notification", isChecked);
            Toast.makeText(this, 
                    isChecked ? "已开启通知" : "已关闭通知", 
                    Toast.LENGTH_SHORT).show();
        });
    }
    
    private void loadSettings() {
        SharedPreferences sp = getSharedPreferences("settings", MODE_PRIVATE);
        switchLocationUpdate.setChecked(sp.getBoolean("location_update", true));
        switchMapSatellite.setChecked(sp.getBoolean("map_satellite", true));
        switchNotification.setChecked(sp.getBoolean("notification", true));
    }
    
    private void saveSettings(String key, boolean value) {
        SharedPreferences.Editor editor = getSharedPreferences("settings", MODE_PRIVATE).edit();
        editor.putBoolean(key, value);
        editor.apply();
    }
} 